
package com.metagurukul.metagranth.model.dao;

import java.util.ArrayList;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;

import com.metagurukul.metagranth.vo.Answer;
import com.metagurukul.metagranth.exception.MetaSqlException;
import com.metagurukul.metagranth.model.common.connection.ConnectionPoolFactory;
import com.metagurukul.metagranth.model.common.dbutil.Util;
/**
 * 
 * @author team metaGranth
 *this class will handle all the answer related operation i.e: save the answer,retrieve the answer etc..
 *
 */
public class AnswerDAO {
	/**
	 * 
	 * @param answerObj stores the object of the answer value object
	 * @return	status whether the answer is saved or not in the database
	 * @throws MetaSqlException
	 */
	public static int saveAnswer(Answer answerObj)throws MetaSqlException {
		//create and retrieve the connection from the connectionPoolFactory
		ConnectionPoolFactory connectionPoolFactory = ConnectionPoolFactory.getInstance();
		Connection con = connectionPoolFactory.checkOut();
		
		int questionID = answerObj.getQid();
		int userID = answerObj.getUid();
		String answer = answerObj.getAnswer();
		//status will store the status of query executed
		int status=0;
		
		
		PreparedStatement pstmt = null;
		String query = "insert into metacubegranth_answer ( QUESTION_IDENTIFICATION_NUMBER, USER_IDENTIFICATION_NUMBER, ANSWER) values(?,?,?)";
		try {
			pstmt = con.prepareStatement(query);
			//set the prepared statement
			pstmt.setInt(1, questionID);
			pstmt.setInt(2, userID);
			pstmt.setString(3, answer);
			
			//will store the number of rows updated
			status = pstmt.executeUpdate();
		} catch (SQLException e) {
			//if there is any sql exception  then the exception is thrown
			//throw new MetaSqlException(e);
			e.printStackTrace();
		} finally {
			//close all the open database connection
			Util.closePreparedStatement(pstmt);
			//checkIn the connection again to the ConnectionPoolFactory
			 connectionPoolFactory.checkIn(con);
		}
		return status;
	}
	
	/**
	 * 
	 * @param questionId stores the question_identification_number
	 * @return	list of the answer value object
	 * this function will return the list of the answer searched on the basis of its question_identification_number
	 * @throws MetaSqlException
	 */
	public static ArrayList<Answer> getAnswerByQuestionId(int questionId)throws MetaSqlException {
		ArrayList<Answer> answerSet = new ArrayList<Answer>();
		//create and retrieve the connection from the connectionPoolFactory
		ConnectionPoolFactory connectionPoolFactory = ConnectionPoolFactory.getInstance();
		Connection con = connectionPoolFactory.checkOut();

		Statement stmt = null;
		String query = "Select * from metacubegranth_answer where QUESTION_IDENTIFICATION_NUMBER = "+ questionId;
		try {
			stmt = con.createStatement();
			ResultSet resultSet = stmt.executeQuery(query);
			//loop will iterate till all the rows are iterated and are stored in arrayList object
			while (resultSet.next()) {
				Answer answerObj = new Answer();
				answerObj.setAid(resultSet.getInt(1));
				answerObj.setQid(resultSet.getInt(2));
				answerObj.setUid(resultSet.getInt(3));
				answerObj.setAnswer(resultSet.getString(4));
				answerObj.setDateofpost(resultSet.getDate(5));
				answerObj.setTitle(resultSet.getString(6));
				answerObj.setIsapproved(resultSet.getBoolean(7));

				answerSet.add(answerObj);
			}

		} catch (SQLException e) {
			//if there is any sql exception  then the exception is thrown
			throw new MetaSqlException(e);
		} finally {
			Util.closeStatement(stmt);
			connectionPoolFactory.checkIn(con);
		}
		return answerSet;
	}
	
	/**
	 * 
	 * @param answerId store the answer_identification_number of the answer whose status has to be updated
	 * @return	the status whether the rows has been updated or not
	 * @throws MetaSqlException
	 */
	public static int updateAnswerStatus(int answerId)throws MetaSqlException {
		//create and retrieve the connection from the connectionPoolFactory
		ConnectionPoolFactory connectionPoolFactory = ConnectionPoolFactory.getInstance();
		Connection con = connectionPoolFactory.checkOut();

		PreparedStatement pstmt = null;
		int noOfRowsUpdated = -1;
		
		String query = "UPDATE metacubegranth_answer SET is_approved = 1 WHERE answer_identification_number = ?";
		try {
			pstmt = con.prepareStatement(query);
			//set the prepared statement
			pstmt.setInt(1, answerId);
			//noOfRowsUpdated store the number of rows updated
			noOfRowsUpdated = pstmt.executeUpdate();
		} catch (SQLException e) {
			//if there is any sql exception  then the exception is thrown
			throw new MetaSqlException(e);
		} finally {
			//close all the open database connection
			Util.closePreparedStatement(pstmt);
			//checkIn the connection again to the ConnectionPoolFactory
			 connectionPoolFactory.checkIn(con);
		}

		return noOfRowsUpdated;
	}

}
